
 ( define array ( make-vector 9 ) )

 ( vector-set! array 0 "READ ARRAY ELEMENTS" )
 ( vector-set! array 1 "SEQUENTIAL SEARCH" )
 ( vector-set! array 2 "BINARY SEARCH" )
 ( vector-set! array 3 "BUBBLE SORT" )
 ( vector-set! array 4 "SELECTION SORT" )
 ( vector-set! array 5 "SHELL SORT" )
 ( vector-set! array 6 "QUICK SORT" )
 ( vector-set! array 7 "DISPLAY ARRAY" )
 ( vector-set! array 8 "QUIT" )

 ( define entered 0 )
 ( define size 1 )
 ( define elmts )
 ( define temp 0 )

 ( define CREATE-DELAY
   ( lambda ( num )
     ( set! num ( * num 2 ) )
     ( do
       ( ( i 0 ( + i 1 ) )
        )
       ( ( = i num )
        )
       ( )
      )
    )
  )

 ( define READ-ELEMENTS
   ( lambda ( )
     ( GOTOXY 2 25 )
     ( display "R E A D   A R R A Y   E L E M E N T S" )
     ( GOTOXY 5 10 )
     ( display "Enter Number Of Elements : " )
     ( set! size ( read ) )
     ( set! elmts ( make-vector size ) )
     ( newline )

     ( do
       ( ( i 0 ( + i 1 ) )
        )
       ( ( = i size ) ( display "" )
        )
       ( display "     Enter Element No [" )
       ( display i )
       ( display "] " )
       ( vector-set! elmts i ( read ) )
       ( newline )
      )
     ( set! entered 1 )
    )
  )

 ( define SEQUENTIAL-SEARCH
   ( lambda ( )
     ( define flag 0 )
     ( define found 0 )

     ( GOTOXY 2 25 )
     ( display "S E Q U E N T I A L   S E A R C H" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 6 10 )
         ( display "Enter the element to search for : " )
         ( set! temp ( read ) )
         ( newline )
         ( GOTOXY 10 10 )
         ( display "Searching ..." )
         ( CREATE-DELAY 2500 )
         ( GOTOXY 10 10 )
         ( display "             " )
         ( GOTOXY 10 0 )

         ( do
           ( ( i 0 ( + i 1 ) )
            )
           ( ( = i size )
              ( if ( = found 0 )
                ( display "Element NOT FOUND !" )
               )
            )
           ( if ( = temp ( vector-ref elmts i ) )
             ( begin
               ( display "           Element FOUND ! Offset is " )
               ( display ( + i 1 ) )
               ( newline )
               ( set! flag 1 )
               ( set! found 1 )
              )
            )
          )
        )
      )
    )
  )

 ( define BINARY-SEARCH
   ( lambda ( )
     ( define low 0 )
     ( define high size )
     ( define mid ( quotient ( + low high ) 2 ) )
     ( define found 0 )

     ( GOTOXY 2 30 )
     ( display "B I N A R Y   S E A R C H" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 6 10 )
         ( display "Enter the element to search for : " )
         ( set! temp ( read ) )
         ( newline )
         ( GOTOXY 10 10 )
         ( display "Searching ..." )
         ( CREATE-DELAY 2500 )
         ( GOTOXY 10 10 )
         ( display "             " )
         ( GOTOXY 10 10 )

         ( do
           ( )
           ( ( = found 1 ) )
           ( if ( >= high low )
             ( begin
               ( if ( = temp ( vector-ref elmts mid ) )
                   ( begin
                     ( set! found 1 )
                     ( display "               Element FOUND ! Offset is " )
                     ( display ( + mid 1 ) )
                    )
                 ( if ( < temp ( vector-ref elmts mid ) )
                    ( set! high ( - mid 1 ) )
                   ( if ( > temp ( vector-ref elmts mid ) )
                     ( set! low ( + mid 1 ) )
                    )
                  )
                )
               ( set! mid ( quotient ( + low high ) 2 ) )
              )
            )
          )
         ( if ( = found 0 )
           ( display "               Element NOT FOUND !" )
          )
        )
      )
    )
  )

 ( define BUBBLE-SORT
   ( lambda ( )
     ( define temp 0 )

     ( GOTOXY 2 30 )
     ( display "B U B B L E   S O R T" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 5 20 )
         ( display "Sorting ..." )
         ( CREATE-DELAY 2500 )
         ( newline )
         ( newline )
         ( newline )
         ( display "          Array is -> " )
         ( display elmts )

         ( do
           ( ( i 0 ( + i 1 ) )
            )
           ( ( = i size ) ( display "" )
            )
           ( do
             ( ( j ( + i 1 ) ( + j 1 ) )
              )
             ( ( = j size ) )
             ( if ( > ( vector-ref elmts i ) ( vector-ref elmts j ) )
               ( begin
                 ( set! temp ( vector-ref elmts j ) )
                 ( vector-set! elmts j ( vector-ref elmts i ) )
                 ( vector-set! elmts i temp )
                 ( newline )
                 ( display "          Array is -> " )
                 ( display elmts )
                )
              )
            )
          )
         ( newline )
         ( newline )
         ( newline )
         ( display "                    Array Sorted ..." )
        )
      )
    )
  )

 ( define SELECTION-SORT
   ( lambda ( )
     ( define temp 0 )

     ( GOTOXY 2 30 )
     ( display "S E L E C T I O N   S O R T" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 5 20 )
         ( display "Sorting ..." )
         ( CREATE-DELAY 2500 )
         ( newline )
         ( newline )
         ( newline )
         ( display "          Array is -> " )
         ( display elmts )

         ( do
           ( ( current 0 ( + current 1 ) ) )
           ( ( = current ( - size 1 ) ) ( display "" ) )
           ( do
             ( ( j ( + current 1 ) ( + j 1 ) ) )
             ( ( = j size ) )
             ( if ( > ( vector-ref elmts current ) ( vector-ref elmts j ) )
               ( begin
                 ( set! temp ( vector-ref elmts current ) )
                 ( vector-set! elmts current ( vector-ref elmts j ) )
                 ( vector-set! elmts j temp )
                 ( newline )
                 ( display "          Array is -> " )
                 ( display elmts )
                )
              )
            )
          )
         ( newline )
         ( newline )
         ( newline )
         ( display "                    Array Sorted ..." )
        )
      )
    )
  )

 ( define SHELL-SORT
   ( lambda ( )
     ( define temp 0 )
     ( define gap ( quotient size 2 ) )
     ( define exchange 1 )

     ( GOTOXY 2 30 )
     ( display "S H E L L   S O R T" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 10 20 )
         ( display "Sorting ..." )
         ( newline )
         ( newline )
         ( newline )
         ( display "          The Array Is -> " )
         ( display elmts )
         ( newline )

         ( do
           ( )
           ( ( = gap ( quotient gap 2 ) ) ( display "" ) )
           ( do
             ( )
             ( ( = exchange 0 ) ( display "" ) )

             ( do
               ( ( i 0 ( + i 1 ) ) )
               ( ( = i ( - size gap ) ) ( display "" ) )
               ( set! exchange 0 )
               ( if ( > ( vector-ref elmts i ) ( vector-ref elmts ( + i gap ) ) )
                  ( begin
                    ( set! temp ( vector-ref elmts i ) )
                    ( vector-set! elmts i ( vector-ref elmts ( + i gap ) ) )
                    ( vector-set! elmts ( + i gap ) temp )
                    ( set! exchange 1 )
                    ( display "          The Array Is -> " )
                    ( display elmts )
                    ( newline )
                   )
                )
              )
            )
           ( set! gap ( quotient gap 2 ) )
          )
         ( newline )
         ( newline )
         ( display "                    Array Sorted ..." )
        )
      )
    )
  )

 ( define QUICK-SORT
   ( lambda ( )
     ( define temp 0 )
     ( define low start )
     ( define high end )
     ( define list-sep  ( vector-ref elmts ( quotient ( + start end ) 2 ) ) )
     ( GOTOXY 2 30 )
     ( display "Q U I C K   S O R T" )
     ( if ( = entered 0 )
        ( begin
          ( GOTOXY 10 10 )
          ( display "THE ARRAY IS EMPTY" )
         )
       ( begin
         ( GOTOXY 10 20 )
         ( display "Sorting ..." )
         ( CREATE-DELAY 2500 )
         ( newline )
         ( newline )
         ( newline )
         ( display "          The Array Is -> " )
         ( display elmts )
         ( newline )

         ( do
           ( )
           ( ( > low high ) )
           ( do
             ( )
             ( ( >= ( vector-ref elmts low ) list-sep ) )
             ( ( set! low ( + low 1 ) )
              )
            )
           ( do
             ( )
             ( ( >= list-sep ( vector-ref elmts high ) ) )
             ( ( set! high ( - high 1 ) )
              )
            )
           ( if ( <= low high )
             ( begin
               ( set! temp ( vector-ref elmts low ) )
               ( vector-set! elmts low ( vector-ref elmts high ) )
               ( vector-set! elmts high temp )
               ( set! low ( + low 1 ) )
               ( set! high ( - high 1 ) )
               ( display "          The Array Is -> " )
               ( display elmts )
               ( newline )
              )
            )
          )

         ( if ( < start high )
           ( QUICK-SORT start high )
          )
         ( if ( < low end )
           ( QUICK-SORT low end )
          )

         ( newline )
         ( newline )
         ( display "Array Sorted ..." )
        )
      )
    )
  )

 ( define DISPLAY-ELEMENTS
   ( lambda ( )
     ( GOTOXY 2 20 )
     ( display "D I S P L A Y   A R R A Y   E L E M E N T S" )
     ( GOTOXY 10 10 )
     ( if ( = entered 0 )
        ( display "THE ARRAY IS EMPTY" )
       ( begin
         ( display "     The Array Is -> " )
         ( display elmts )
        )
      )
    )
  )

 ( define SORTING
   ( lambda ( )
     ( define choice 0 )

     ( load "BOXES.BSP" )
     ( load "GENMENU.BSP" )

     ( do
       ( )
       ( ( = choice 8 ) ( CLS )
                        ( GOTOXY 1 20 )
                        ( display "GoodBye !" )
        )
       ( cls )
       ( SOLID-BOX1 1 15 6 50 )
       ( SHADOW 1 15 6 50 )
       ( GOTOXY 3 23 )
       ( display "S O R T I N G   T E C H N I Q U E S" )
       ( GOTOXY 5 23 )
       ( display "     (c) Bipin.S.Patwardhan" )
       ( set! choice ( MENU 12 30 array 9 19 ) )
       ( case choice
         ( ( 0 ) ( CLS )
                 ( READ-ELEMENTS )
                 ( read )
                 ( read-char )
          )
         ( ( 1 ) ( CLS )
                 ( SEQUENTIAL-SEARCH )
                 ( read )
                 ( read-char )
          )
         ( ( 2 ) ( CLS )
                 ( BINARY-SEARCH )
                 ( read )
                 ( read-char )
          )
         ( ( 3 ) ( CLS )
                 ( BUBBLE-SORT )
                 ( read )
                 ( read-char )
          )
         ( ( 4 ) ( CLS )
                 ( SELECTION-SORT )
                 ( read )
                 ( read-char )
          )
         ( ( 5 ) ( CLS )
                 ( SHELL-SORT )
                 ( read )
                 ( read-char )
          )
         ( ( 6 ) ( CLS )
                 ( QUICK-SORT )
                 ( read )
                 ( read-char )
          )
         ( ( 7 ) ( CLS )
                 ( DISPLAY-ELEMENTS )
                 ( read )
                 ( read-char )
          )
        )
      )
    )
  )

 ( SORTING )
